package com.googlecode.slm.web.springweb;


import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.slm.domain.web.UserNode;
import com.googlecode.slm.service.UserService;
import com.googlecode.slm.util.CONSTANTS;
import com.googlecode.slm.web.BasicControler;


/**
 * 
 * @author liguangjian
 *
 */
@Controller
@RequestMapping(value = "/usermanger")
public class UserMangerControler extends BasicControler  {
	
	
	@Autowired
	@Qualifier(value = "userService")
	private UserService userService;
	
	
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/welcome.form")
	public String welcome(){
		return CONSTANTS.VIEWURL_USERMANGER;
	}

	@RequestMapping(value = "/query.form")
	@ResponseBody
	public String getUser(int page, int rows){
		JSONObject json = new JSONObject();   
		List<UserNode> list = new ArrayList<UserNode>();
		int count = userService.getAllUser(page, rows, list);
		JSONArray array = new JSONArray(list, false);   
		try {
			json.put("total", count);   
			json.put("rows", array);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toString();
	}
	@RequestMapping(value = "/delete.form")
	@ResponseBody
	public String delete(int id){
		if (userService.deleteUser(id)>0){
			return "成功";
		} 
		return "失败";
	}

}
